package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.huike.business.domain.vo.BusinessChangeVo;
import com.huike.report.domain.vo.ClueFunnelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    
    @Autowired
    private ReportMapper reportMpper;


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId= request.getDeptId();
        TbAssignRecord tbAssignRecord=new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }



    @Override
    public ClueFunnelVo getSomeNums(IndexStatisticsVo request) {

        ClueFunnelVo clueFunnelVo = new ClueFunnelVo();
        //获取线索数
        clueFunnelVo.setCluesNums(clueMapper.getNums(request));
        //获取有效线索数
        clueFunnelVo.setEffectiveCluesNums(clueMapper.geteffectiveClues(request));
        //获取商机数
        clueFunnelVo.setBusinessNums(businessMapper.countAllClue(request));
        //获取合同数
        clueFunnelVo.setContractNums(contractMapper.countStatisticByTime(request));

        return clueFunnelVo;
    }
//
//        public List<Map<String,Object>>businessChangeStatisticsForIndex(IndexStatisticsVo request){
//        int allBusiness =businessMapper.countAllBusiness(request.getBeginCreateTime(),request.getEndCreateTime());
//            List<Map<String, Object>> list = businessMapper.businessChangeStatistics(request);
//            for (Map<String, Object> datum : list) {
//                Long num = (Long) datum.get("num");
//                datum.put("radio",getRadio(allBusiness,num));
//
//            }
//            return  list;
//        }

    //商机龙虎榜
    @Override
    public List<BusinessChangeVo> findBusinessChangeStatisticsVo(String beginCreateTime, String endCreateTime) {
        //获得姓名、部门、合同数、部门id
        List<BusinessChangeVo> voList = reportMpper.findBusinessChangeStatisticsVo(beginCreateTime, endCreateTime);
        //获得商机总数
        Long countBusiness = reportMpper.countBusiness(beginCreateTime, endCreateTime);
        //进行精度规范
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        //设置精确几位小数
        df.setMaximumFractionDigits(0);
        //设置舍入方式：四舍五入
        df.setRoundingMode(RoundingMode.HALF_UP);
        voList.stream().map(vo -> {
            //获得转化率，保留0位小数，四舍五入
            Integer radio = Integer.parseInt(df.format(vo.getNum() * 100 / countBusiness));
            //设置转化率
            vo.setRadio(radio);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }


    private BigDecimal getRadio(Integer all,Long num) {
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }
    //线索转化龙虎榜
    @Override
    public List<BusinessChangeVo> salesStatistic(String beginCreateTime, String endCreateTime) {
        //获得姓名、部门、合同数、部门id
        List<BusinessChangeVo> businessChangeVos = reportMpper.salesStatistic(beginCreateTime, endCreateTime);
        //获得商机总数
        Integer numbers = reportMpper.countClue(beginCreateTime, endCreateTime);
        //进行精度规范
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        //设置精确几位小数
        df.setMaximumFractionDigits(0);
        //设置舍入方式：四舍五入
        df.setRoundingMode(RoundingMode.HALF_UP);
        businessChangeVos.stream().map(vo -> {
            //获得转化率，保留0位小数，四舍五入
            Integer radio = Integer.parseInt(df.format(vo.getNum() * 100 / numbers));
            //设置转化率
            vo.setRadio(radio);
            return vo;
        }).collect(Collectors.toList());
        return businessChangeVos;
    }
}